/**
 * Project Name:dsrs-systemservice-web
 * File Name:LoginRuleController.java
 * Package Name:com.bwda.dsrs.systemservice.controller;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.controller;

import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.pwdrule.PwdRuleCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.loginrule.LoginRuleUpdateForm;
import com.bwda.dsrs.systemservice.domain.form.pwdrule.PwdRuleQueryForm;
import com.bwda.dsrs.systemservice.domain.po.pwdrule.PwdRulePo;
import com.bwda.dsrs.systemservice.domain.vo.pwdrule.PwdRuleVo;
import com.bwda.dsrs.systemservice.service.LoginRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 登录规则
 *
 * @author liangjianyong
 * @date 2019/4/22
 */
@RestController
@RequestMapping("/s/loginrule")
@Api(value = "/s/loginrule", tags = "登录安全规则")
public class LoginRuleController extends BaseController {

    @Autowired
    private LoginRuleService loginRuleService;

    @NeedRole("sysadminrole")
    @ApiOperation(value = "查询登录安全规则", notes = "查询登录安全规则", httpMethod = "GET")
    @Loggable(descp = "查询登录安全规则",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<PwdRuleVo> query() throws SystemServiceException {
        PwdRuleCondition condition = new PwdRuleCondition();
        condition.setDelFlag(Constants.NOT_DELETED);
        List<PwdRulePo> poList = loginRuleService.queryList(condition);
        List<PwdRuleVo> voList = CopyUtil.transfer(poList, PwdRuleVo.class);
        if(voList==null||voList.isEmpty()){
            return getFailResult("没有设置安全规则");
        }
        return getSuccessResult(voList.get(0));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改登录安全规则", notes = "修改登录安全规则", httpMethod = "POST")
    @Loggable(descp = "修改登录安全规则",type = LogTypeEnum.UPDATE,include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid LoginRuleUpdateForm form) throws SystemServiceException {
        PwdRulePo po = CopyUtil.transfer(form, PwdRulePo.class);
        //查询当前登录规则
        PwdRuleCondition condition=new PwdRuleCondition();
        condition.setDelFlag(Constants.NOT_DELETED);
        List<PwdRulePo> pwdRulePoList=loginRuleService.queryList(condition);
        if(pwdRulePoList==null||pwdRulePoList.isEmpty()){
            //新增
            loginRuleService.insert(po);
        }else{
            //修改
            Integer id=pwdRulePoList.get(0).getId();
            po.setId(id);
            loginRuleService.update(po);
        }
        return getSuccessResult();
    }

    /**
     * PwdRuleQueryForm转换为PwdRuleCondition
     *
     * @param form
     * @return
     */
    private PwdRuleCondition getConditionByQueryForm(PwdRuleQueryForm form) {
        PwdRuleCondition condition = CopyUtil.transfer(form, PwdRuleCondition.class);
        return condition;
    }

}
